package com.zy.quartz.task;

import com.zy.business.domain.TbPunchCard;
import com.zy.business.mapper.HolidayMapper;
import com.zy.business.service.HolidayService;
import com.zy.business.service.PunchCardService;
import com.zy.common.core.domain.entity.SysUser;
import com.zy.common.utils.DateUtil;
import com.zy.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @description:定时生成考勤记录
 * @author: whf
 * @create: 2022-04-29 17:31
 **/
@Component("createTask")
public class CreateTask {

    @Autowired
    private PunchCardService punchCardService;

    @Resource
    private SysUserMapper userMapper;

    @Autowired
    private HolidayService holidayService;

    @Resource
    private HolidayMapper holidayMapper;

    public void createObject1() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1){
            List<SysUser> userList = userMapper.getAllUserWithOutBoss();
            if(userList!=null && userList.size()>0){
                for(SysUser user:userList){
                    TbPunchCard punchCard = new TbPunchCard();
                    punchCard.setUserId(user.getUserId());
                    punchCard.setDingUserId(user.getDingUserId());
                    punchCard.setDeptId(user.getDeptId());
                    punchCard.setCreateTime(new Date());
                    punchCard.setState(1);
                    punchCardService.save(punchCard);
                }
            }
        }
    }

    /**
     * 中午12点生成当天中午的下班打卡提醒
     */
    public void createObject2() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1){
            List<SysUser> userList = userMapper.getAllUserWithOutBoss();
            if(userList!=null && userList.size()>0){
                for(SysUser user:userList){
                    TbPunchCard punchCard = new TbPunchCard();
                    punchCard.setUserId(user.getUserId());
                    punchCard.setDingUserId(user.getDingUserId());
                    punchCard.setDeptId(user.getDeptId());
                    punchCard.setCreateTime(new Date());
                    punchCard.setState(2);
                    punchCardService.save(punchCard);
                }
            }
        }
    }

    /**
     * 下午1点生成当天下午的上班打卡提醒
     */
    public void createObject3() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1){
            List<SysUser> userList = userMapper.getAllUserWithOutBoss();
            if(userList!=null && userList.size()>0){
                for(SysUser user:userList){
                    TbPunchCard punchCard = new TbPunchCard();
                    punchCard.setUserId(user.getUserId());
                    punchCard.setDingUserId(user.getDingUserId());
                    punchCard.setDeptId(user.getDeptId());
                    punchCard.setCreateTime(new Date());
                    punchCard.setState(3);
                    punchCardService.save(punchCard);
                }
            }
        }
    }

    /**
     * 下午6点生成当天下午的下班打卡提醒
     */
    public void createObject4() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1){
            List<SysUser> userList = userMapper.getAllUserWithOutBoss();
            if(userList!=null && userList.size()>0){
                for(SysUser user:userList){
                    TbPunchCard punchCard = new TbPunchCard();
                    punchCard.setUserId(user.getUserId());
                    punchCard.setDingUserId(user.getDingUserId());
                    punchCard.setDeptId(user.getDeptId());
                    punchCard.setCreateTime(new Date());
                    punchCard.setState(4);
                    punchCardService.save(punchCard);
                }
            }
        }
    }

    /**
     * 每月25号生成下月的工作日
     */
    public void createHoliday(){
        int currentMonth = DateUtil.getCurrentMonth();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(aCalendar.MONTH, currentMonth);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH)+1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate = String.valueOf(year)+"-"+month+"-"+i;
            Date parse = null;
            try {
                parse = simpleDateFormat.parse(aDate);
                holidayService.createHoliday(parse);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}
